const app = getApp()

Page({
  data: {
    userInfo: null,
    user: {
      // id: "ccc",
      // username: "方糕",
      // sex: 2,
      // birthday: "2002-03-03",
      // headimg: "/assets/avatars/avatar1.jpg",
      // department: "",
      // address: ""
    }
  },

  onShow: function () {
    wx.setNavigationBarTitle({
      title: "我的"
    });
    this.setData({
      userInfo: app.globalData.userInfo
    })
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        //console.log(res.data.data);
        that.setData({
          user: res.data.data
        })
      }
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        //console.log(res)
        this.setData({
          userInfo: res.userInfo,
        })
        app.globalData.userInfo = this.data.userInfo
        //console.log(app.globalData.userInfo);
        this.initUserData()
      }
    })
  },

  initUserData() {
    var loginAccount = app.globalData.loginAccount
    var nickName = this.data.userInfo.nickName
    var gender = this.data.userInfo.gender
    var avatarUrl = this.data.userInfo.avatarUrl
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/initAccount',
      method: 'POST',
      data: {
        id: loginAccount,
        username: nickName,
        sex: gender,
        headimg: avatarUrl
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //console.log(res.data.data);
        wx.request({
          url: 'http://localhost:8888/account/getAccount',
          method: 'GET',
          data: {
            id: loginAccount
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            app.globalData.userImg = res.data.data.headimg
            that.setData({
              userImg: res.data.data.headimg,
              userNickname: res.data.data.username
            })
          }
        })
      }
    })
  },

  noLogin() {
    wx.showToast({
      title: '请先登录',
      icon: 'error'
    })
  },

  toEdit() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      wx.navigateTo({
        url: '../user-edit/edit'
      })
    }
  },

  toCollection() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      wx.navigateTo({
        url: '../secondhand-collections/collections',
      })
    }
  },

  toShop() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.shopId = app.globalData.loginAccount
      app.globalData.shopIsMine = true
      wx.navigateTo({
        url: '../secondhand-shop/shop',
      })
    }
  },

  toCart() {
    wx.navigateTo({
      url: '../secondhand-cart/cart',
    })
  },

  toOrder() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.isBuying = true
      wx.navigateTo({
        url: '../order-lists/orderlist',
      })
    }
  },

  toSale() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.isBuying = false
      wx.navigateTo({
        url: '../order-lists/orderlist',
      })
    }
  },

  toMyPost() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.viewPostType = 1
      wx.navigateTo({
        url: '../my-post/home',
      })
    }
  },

  toMyLike() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.viewPostType = 2
      wx.navigateTo({
        url: '../my-post/home',
      })
    }
  },

  toMyComment() {
    if (this.data.userInfo == null) {
      this.noLogin()
    } else {
      app.globalData.viewPostType = 3
      wx.navigateTo({
        url: '../my-post/home',
      })
    }
  }
})